package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.ChiTietKhuyenMaiDao;
import com.tt.fashionWebSite.dao.ChiTietSanPhamDao;
import com.tt.fashionWebSite.dao.KhuyenMaiDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_DealService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("ad_dealService")
public class Ad_DealServiceImp extends GenericService implements Ad_DealService {
    @Autowired
    @Qualifier("khuyenMai")
    private KhuyenMaiDao khuyenMaiDao;

    @Autowired
    @Qualifier("chiTietKhuyenMai")
    private ChiTietKhuyenMaiDao chiTietKhuyenMaiDao;

    @Autowired
    @Qualifier("thongTinThanhVien")
    private ThongTinThanhVienDao thongTinThanhVienDao;

    @Autowired
    @Qualifier("chiTietSanPham")
    private ChiTietSanPhamDao chiTietSanPhamDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminDeal(KhuyenMaiDto dealDto) {
        KhuyenMai khuyenMai = khuyenMaiDao.findById(dealDto.getMaKM());
        if(khuyenMai == null){
            khuyenMai = new KhuyenMai();
            khuyenMai.setMaKM(dealDto.getMaKM());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(dealDto.getThongTinThanhVien().getMaTV());
            khuyenMai.setThongTinThanhVien(thongTinThanhVien);
            khuyenMai.setTenKM(dealDto.getTenKM());
            khuyenMai.setNgayBD(dealDto.getNgayBD());
            khuyenMai.setNgayKT(dealDto.getNgayKT());
            khuyenMai.setNgayTaoKM(dealDto.getNgayTaoKM());
            khuyenMaiDao.insert(khuyenMai);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    public List<KhuyenMaiDto> getAllDeal() {
        return mapper.mapCollection(khuyenMaiDao.getAllDeal(), KhuyenMaiDto.class);
    }

    @Override
    public List<ChiTietKhuyenMaiDto> getAllDealDetail() {
        return mapper.mapCollection(chiTietKhuyenMaiDao.getAllDealDetail(), ChiTietKhuyenMaiDto.class);
    }

    public KhuyenMaiDto getDealInfoEdit(KhuyenMaiDto dealDto){
        KhuyenMai khuyenMai = khuyenMaiDao.findById(dealDto.getMaKM());
        KhuyenMaiDto khuyenMaiDto = mapper.map(khuyenMai, KhuyenMaiDto.class,KHUYEN_MAI_MAP_ID);
        return  khuyenMaiDto;
    }

    public ChiTietKhuyenMaiDto getDealDetailInfoEdit(ChiTietKhuyenMaiDto dealdetailDto){
        ChiTietKhuyenMai chiTietKhuyenMai = chiTietKhuyenMaiDao.findById(dealdetailDto.getMaCTKM());
        ChiTietKhuyenMaiDto chiTietKhuyenMaiDto = mapper.map(chiTietKhuyenMai, ChiTietKhuyenMaiDto.class,CHI_TIET_KHUYEN_MAI_MAP_ID);
        return  chiTietKhuyenMaiDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public KhuyenMaiDto registerDealEdit(KhuyenMaiDto dealDto) {
        KhuyenMai khuyenMai = khuyenMaiDao.findById(dealDto.getMaKM());
        try {
            khuyenMai.setMaKM(dealDto.getMaKM());
            khuyenMai.setNgayTaoKM(dealDto.getNgayTaoKM());
            khuyenMai.setNgayKT(dealDto.getNgayKT());
            khuyenMai.setNgayBD(dealDto.getNgayBD());
            khuyenMai.setTenKM(dealDto.getTenKM());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(dealDto.getThongTinThanhVien().getMaTV());
            khuyenMai.setThongTinThanhVien(thongTinThanhVien);
            khuyenMaiDao.update(khuyenMai);
            return dealDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ChiTietKhuyenMaiDto registerDealDetailEdit(ChiTietKhuyenMaiDto dealdetailDto) {
        ChiTietKhuyenMai chiTietKhuyenMai = chiTietKhuyenMaiDao.findById(dealdetailDto.getMaCTKM());
        try {
            chiTietKhuyenMai.setMaCTKM(dealdetailDto.getMaCTKM());
            chiTietKhuyenMai.setPhanTram(dealdetailDto.getPhanTram());

            ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(dealdetailDto.getChiTietSanPham().getMaCTSP());
            chiTietKhuyenMai.setChiTietSanPham(chiTietSanPham);

            KhuyenMai khuyenMai = khuyenMaiDao.findById(dealdetailDto.getKhuyenMai().getMaKM());
            chiTietKhuyenMai.setKhuyenMai(khuyenMai);

            khuyenMaiDao.update(khuyenMai);
            return dealdetailDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public ChiTietKhuyenMaiDto getDataDetailOfDealCommon(String dealId){
        ChiTietKhuyenMai chiTietKhuyenMai = new ChiTietKhuyenMai();
        chiTietKhuyenMai = chiTietKhuyenMaiDao.findById(dealId);
        ChiTietKhuyenMaiDto chiTietKhuyenMaiDto = new ChiTietKhuyenMaiDto();
        if (chiTietKhuyenMai != null) {
            chiTietKhuyenMaiDto = mapper.map(chiTietKhuyenMai, ChiTietKhuyenMaiDto.class, CHI_TIET_KHUYEN_MAI_MAP_ID);
        } else {
            return null;
        }
        return chiTietKhuyenMaiDto;
    }

    @Override
    public List<ThongTinThanhVienDto> getAllMemberShowInDeal() {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    @Override
    public List<ThongTinThanhVienDto> getAllMemberShowInDealCommon() {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public List<ChiTietSanPhamDto> getAllProductDetailCodeInDeal () {
        return mapper.mapCollection(chiTietSanPhamDao.getAllProductDetail(), ChiTietSanPhamDto.class);
    }

    public List<ChiTietSanPhamDto> getAllProductDetailShowInDetail () {
        return mapper.mapCollection(chiTietSanPhamDao.getAllProductDetail(), ChiTietSanPhamDto.class);
    }

    @Override
    public List<KhuyenMaiDto> getAllDealDetailShowInDetail() {
        return mapper.mapCollection(khuyenMaiDao.getAllDeal(), KhuyenMaiDto.class);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertProductDetailForDeal(List<ChiTietSanPhamDto> dealDto, String maKhuyenMai, Float percent) {
//        KhuyenMai khuyenMai = khuyenMaiDao.findById(dealDto.getMaKM());
        KhuyenMai khuyenMai = khuyenMaiDao.findById(maKhuyenMai);
        try {
            for(int i = 0; i < dealDto.size(); i++) {
                ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(dealDto.get(i).getMaCTSP());
               ChiTietKhuyenMai chiTietKhuyenMai = new ChiTietKhuyenMai();
                chiTietKhuyenMai.setKhuyenMai(khuyenMai);
                chiTietKhuyenMai.setChiTietSanPham(chiTietSanPham);
                chiTietKhuyenMai.setPhanTram(percent);

                chiTietKhuyenMaiDao.insert(chiTietKhuyenMai);
            }
            return JsonResponseCode.SUCCESS;
        }
        catch (Exception ex) {
            return JsonResponseCode.FAILED;
        }
    }
}
